package main

import (
	"database/sql"
	"fmt"
	"strconv"

	"github.com/PuerkitoBio/goquery"
	"github.com/gocolly/colly"
	_ "github.com/lib/pq"
)

var db *sql.DB

func main() {
	db, _ = sql.Open("postgres", "host=127.0.0.1 port=5432 user=postgres password=admin dbname=xx sslmode=disable")
	for i := 1; i < 201; i++ {
		page := strconv.Itoa(i)
		url := "https://travel.qunar.com/travelbook/list.htm?page=" + page + "&order=hot_ctime&month=1_2_3"
		getTraval(url, i)
	}
	for i := 1; i < 201; i++ {
		page := strconv.Itoa(i)
		url := "https://travel.qunar.com/travelbook/list.htm?page=" + page + "&order=hot_ctime&month=4_5_6"
		getTraval(url, i)
	}
	for i := 1; i < 201; i++ {
		page := strconv.Itoa(i)
		url := "https://travel.qunar.com/travelbook/list.htm?page=" + page + "&order=hot_ctime&month=7_8_9"
		getTraval(url, i)
	}
	for i := 1; i < 201; i++ {
		page := strconv.Itoa(i)
		url := "https://travel.qunar.com/travelbook/list.htm?page=" + page + "&order=hot_ctime&month=10_11_12"
		getTraval(url, i)
	}
	DataClean()
}
func getTraval(url string, num int) {
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"))
	// 遍历所有的a标签，如果有href属性就回调
	c.OnHTML(".list_item", func(e *colly.HTMLElement) {
		var (
			name           string
			date           string
			days           string
			photo_nums     string
			fee            string
			people         string
			fee_int        int
			photo_nums_int int
			err            error
		)
		e.DOM.Find(".tit").Each(func(i int, s *goquery.Selection) {
			name = s.Find("a").Text()
		})
		e.DOM.Find(".user_info").Each(func(i int, s *goquery.Selection) {
			date = s.Find(".date").Text()
			days = s.Find(".days").Text()
			photo_nums = s.Find(".photo_nums").Text()
			fee = s.Find(".fee").Text()
			people = s.Find(".people").Text()
		})
		if len(photo_nums) > 0 {
			fmt.Println(photo_nums)
			photo_nums = photo_nums[0 : len(photo_nums)-9]
			fmt.Println(photo_nums)
			photo_nums_int, err = strconv.Atoi(photo_nums)
			if err != nil {
				panic(err)
			}
		}
		if len(fee) > 0 {
			fee = fee[6 : len(fee)-3]
			fee_int, _ = strconv.Atoi(fee)
		}

		db.QueryRow("insert into \"data\"(name,date,days,photo_nums,fee,people) values($1,$2,$3,$4,$5,$6);", name, date, days, photo_nums_int, fee_int, people).Scan()
	})
	//c.OnResponse(func(r *colly.Response) {
	//})
	// 在请求之前打印“Visiting xxx”
	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL.String())
	})
	c.Visit(url)
	fmt.Println("page:", num)
	//c.Visit("https://travel.qunar.com/travelbook/list.htm?page=1&order=hot_heat&month=1_2_3")
}
